<?php


namespace Tbf\Bundle\CallbackSerializerBundle;


use Symfony\Component\DependencyInjection\ContainerInterface;
use Tbf\Bundle\CallbackSerializerBundle\Wrapper\CallbackWrapper;
use Tbf\Bundle\CallbackSerializerBundle\Wrapper\DicServiceWrapper;
use Tbf\Bundle\SerializerBundle\Serializer;

class CallbackSerializer{
    protected $container;
    protected $serializer;
    function __construct(ContainerInterface $container,
                Serializer $serializer
            ){
        $this->container = $container;
        $this->serializer = $serializer;
    }

    /**
     * @param string $service_id a service #service
     * @param $name
     * @return DicServiceWrapper
     */
    function createDicServiceCallback($service_id,$name){
        $obj = new DicServiceWrapper($service_id,$name);
        $obj->setContainer($this->container);
        return new CallbackWrapper($obj);
    }

    /**
     * @param CallbackInterface $callback
     * @return string
     */
    function toString(CallbackInterface $callback){
        if (!$callback instanceof CallbackWrapper){
            return $this->serializer->toArray(new CallbackWrapper($callback));
        }
        return $this->serializer->toArray($callback);
    }

    /**
     * @param string $string
     * @return CallbackWrapper
     */
    function fromString($string){
        return $this->serializer->newFromArray('\Tbf\Bundle\CallbackSerializerBundle\Wrapper\CallbackWrapper',$string);
    }
}